
package com.sailing.lianxi.thread.pr;

import java.util.ArrayList;
import java.util.List;

import com.sailing.lianxi.util.thread.ThreadTool;

public class DaemonThread implements Runnable {

    static List<String> list = new ArrayList<>();
    @Override
    public void run() {
        System.out.println("子线程开始："+Thread.currentThread().getName());
        list.add(Thread.currentThread().getName());
        System.out.println("子线程结束："+Thread.currentThread().getName());
    }
    
    public static void main(String[] args) {
        System.out.println("主线程开始："+Thread.currentThread().getName());
        for(int i=0;i<5;i++){
            DaemonThread daemonThread = new DaemonThread();
            Thread th = new Thread(daemonThread);
//            th.setDaemon(true);
            th.start();
        }
        //保证所有子线程启动
        try {
            Thread.sleep(5*1000);
        } catch (InterruptedException e) {
            
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }
        System.out.println("主线程结束："+Thread.currentThread().getName());
        for (int i = 0; i <list.size(); i++) {
            System.out.println("===="+ThreadTool.findThreadByName(list.get(i)).isAlive());
        }
    }
    
}

